package com.jiliang.common.generatorKey.impl;

import com.jiliang.common.date.DateDsConstant;
import com.jiliang.common.generatorKey.GeneratorKeyService;
import com.jiliang.common.redis.RedisLock;
import com.jiliang.common.redis.RedisService;
import com.jiliang.common.string.StringCheckDsUtil;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @author: ZhuPengFei
 * @explain: 生成系统key实现类
 * @since: 2018/8/17 16:52
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class GeneratorKeyServiceImpl implements GeneratorKeyService {

    @Resource
    RedisService redisService;

    /**
     * 生成用户boId
     *
     * @return java.lang.Long
     * @author
     * @since 2018/8/17 16:53
     */
    @Override
    public Long getMemberBoId() {
//        Long boId = getLong(MemberConstant.MEMBER_MOUDLE_ID ,SystemConstant.REDIS_MEMBER_BOID_PREF);
//        return boId;
        return null;
    }

    /**
     * 生成泵站id
     * @return
     */
    @Override
    public Long getPumpBoId() {
        return getLong("2023","redis:generator:PumpId:");
    }

    /**
     * 获取兑换记录id
     * @param
     * @return java.lang.Long
     * @author lpp
     * @date 2019/1/7
     */
    @Override
    public Long getExchangeRecordId() {
        return getLong("1901","redis:pointShop:recordId:");
    }

    @Override
    public Long getManufacturerId() {
        Long test = getLong("2103", "redis:generator:ManufacturerId:");
        return test;
    }

    @Override
    public Long getBillTaxpayInfoId() {
        Long test = getLong("3002", "redis:generator:BillTaxpayInfoId:");
        return test;
    }

    @Override
    public Long getBillInvoiceInfoId() {
        Long test = getLong("3003", "redis:generator:BillInvoiceInfoId:");
        return test;
    }
    @Override
    public Long getHelpDeskComplaintInformId() {
        Long test = getLong("4001", "redis:generator:helpDeskComplaintInformId:");
        return test;
    }

    public Long getLong(String model, String keyStr) {
        Long number = null;
        String time = DateFormatUtils.format(new Date(), DateDsConstant.DATE_FORMAT_YYMMDDHHMM);
        RedisLock rLock = redisService.getRLock(time);
        String key = keyStr + time;
        try {
            rLock.lock();
            //模块id+定义号  判断当前分钟内缓存是否存在数据 1 如果存在则获取序列号 2  如果不存在则生成0001
            String val = (String) redisService.get(key);
            int temp = StringCheckDsUtil.isNotBlank(val) ? (Integer.parseInt(val) + 1) : 1;
            redisService.set(key, String.valueOf(temp), 1L, TimeUnit.MINUTES);
            String format = String.format("%04d", temp);
            number = Long.parseLong(model + time + format);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            rLock.unlock();
        }
        return number;
    }


}
